package com.itcast.dao.Impl;

import com.itcast.dao.StudentDao;
import com.itcast.domain.PageBean;
import com.itcast.domain.Student;
import com.itcast.utils.JdbcDruidUtils2;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @param
 * @return
 */
public class StudentDaoImpl implements StudentDao {
    //创建工具类对象复用
    JdbcTemplate jdbcTemplate = new JdbcTemplate(JdbcDruidUtils2.getDataSource());
    @Override
    public List<Student> findAll() {
        //定义返回值
        List<Student> list = new ArrayList<>();
        //利用工具类对象,执行SQL语句
        String sql = "select * from Student_01";
         list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Student>(Student.class));
        return list;
    }

    @Override
    public int add(Student student) {
        //定义返回值
        int i=0;
        //定义SQL语句
        String sql = "insert into Student_01 values (?,?,?,?,?,?,?)";
        //使用框架技术实现添加
         i = jdbcTemplate.update(sql, null, student.getName(), student.getGender(), student.getAge(), student.getAddress(), student.getQq(), student.getEmail());
        //这里的返回值无所谓，习惯而已
         return i;
    }

    @Override
    public Student findById(int id) {
        //定义返回值
        Student student = new Student();
        //定义sql语句
        String sql = "select * from Student_01 where id=?";
        //执行
         student = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Student>(Student.class), id);
        return student;
    }

    @Override
    public int delete(int id) {
        //定义返回值
        int i =0;
        //定义SQL
        String sql = "delete from Student_01 where id =?";
        //执行
         i = jdbcTemplate.update(sql, id);
        return i;
    }

    @Override
    public int update(Student student) {
        //定义返回值
        int i =0;
        //定义SQL
        String sql = "update Student_01 set name=? , gender=? , age =? , address = ? , qq = ? , Email = ? where id = ?  ";
        i=jdbcTemplate.update(sql,student.getName(),student.getGender(),student.getAge(),student.getAddress(),student.getQq(),student.getEmail(),student.getId());
        return i;
    }

    @Override
    public PageBean<Student> findByPage(int currentPage, int rows) {
        //定义返回值
        PageBean<Student> pageBean = new PageBean<>();
        //调用方法查询总记录数
        int totalCount = findCount();
        //有了总记录数，有了每页显示的条目数，就可以求出总页码数
        int totalPage=totalCount%rows==0?totalCount/rows:(totalCount/rows)+1;
        //再来求开始索引 = (当前页码-1) * 每页显示的条数
        int start=(currentPage-1)*rows;
        //定义SQL
        String sql = "select * from Student_01 limit ? ,?";
        //开始求最后一个成员变量值
        List<Student> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Student>(Student.class), start, rows);
        //所有值都已到手，开始封装对象
        pageBean.setRows(rows);
        pageBean.setCurrentPage(currentPage);
        pageBean.setTotalCount(totalCount);
        pageBean.setList(list);
        pageBean.setTotalPage(totalPage);
        return pageBean;
    }

    @Override
    public boolean login(String username, int password) {
        //定义返回值
        boolean flag=false;
        //定义SQL
        String sql = "select * from User where username = ? and password = ?";
        //执行
        try {//抓一下异常
            Student student = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Student>(Student.class), username, password);
            //如果查询结果不为空，则表示登录成功
            if(student!=null){
                flag=true;
            }
        }catch (Exception e){}
        finally {
            return flag;
        }
    }

    private int findCount() {
        //定义返回值
        int totalCount =0;
        //定义SQL
        String sql = "select count(*) from Student_01 ";
        //执行
        totalCount = jdbcTemplate.queryForObject(sql, Integer.class);
        return totalCount;
    }
}
